package com.example.sdk.exception;

/**
 * Created by roger on 2019/1/8.
 */
public class MoPlusException extends Exception  {

    /**
     *
     */
    private static final long serialVersionUID = -3769656490878599184L;

    /**
     * 错误代码,默认为未知错误
     */
    protected String errorCode = "UNKNOW_ERROR";

    /**
     * 错误信息中的参数
     */
    protected String[] errorArgs = null;

    /**
     * 兼容纯错误信息，不含error code,errorArgs的情况
     */
    protected String errorMessage = null;

    protected String businessLabel = null;

    public MoPlusException() {
        super();
    }

    public MoPlusException(Throwable cause) {
        super(cause);
    }

    public MoPlusException(String messsage, Throwable cause) {
        super(messsage, cause);
    }

    public MoPlusException(String errorCode, String[] errorArgs) {
        this.errorCode = errorCode;
        this.errorArgs = errorArgs;
    }

    public MoPlusException(String businessLabel) {
        this.businessLabel = businessLabel;
    }

    public MoPlusException(String errorCode, String[] errorArgs, Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.errorArgs = errorArgs;
    }

    public MoPlusException(String errorCode, String errorArg, Throwable cause) {
        super(cause);
        this.errorCode = errorCode;
        this.errorArgs = new String[] { errorArg };
    }

    /**
     * @param errorCode
     *            错误码
     * @param errorMessage
     *            错误信息
     */
    public MoPlusException(String errorCode, String errorMessage) {
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    public MoPlusException(int errorCode, String errorMessage) {
        this.errorCode = String.valueOf(errorCode);
        this.errorMessage = errorMessage;
    }

    public MoPlusException(int errorCode, String businessLabel, String errorMessage) {
        this.errorCode = String.valueOf(errorCode);
        this.businessLabel = businessLabel;
        this.errorMessage = errorMessage;
    }

    /**
     * @param errorMessage
     *            错误信息
     */
    // public BusinessException(String errorMessage) {
    // this.errorMessage = errorMessage;
    // }

    /**
     * 获得出错信息. 读取i18N properties文件中Error Code对应的message,并组合参数获得i18n的出错信息
     */
    public String getMessage() {
        // 如果errorMessage不为null,直接返回出错信息.
        if (errorMessage != null) {
            return errorMessage;
        } else {

            errorMessage = "error";
        }

        return errorMessage;

    }

    public String getBusinessLabel() {

        if (businessLabel != null) {
            return businessLabel;
        } else {

            businessLabel = "";
        }

        return businessLabel;
    }

    public String getErrorCode() {
        return errorCode;
    }
}
